//
//  DorrSystemSDCardViewController.m
//  RONGAO
//
//  Created by wei wang on 2017/3/13.
//  Copyright © 2017年 wei wang. All rights reserved.
//

#import "DoorSystemSDCardViewController.h"

@interface DoorSystemSDCardViewController ()<UITableViewDelegate,UITableViewDataSource,MyCameraDelegate>
/** tableview **/
@property(nonatomic,strong) UITableView *tableView;

/** titleArray **/
@property(nonatomic,strong) NSArray *titleArray;

/**
 总容量
 */
@property (nonatomic) NSInteger totalSize;

/**
 可用容量
 */
@property (nonatomic) NSInteger freeSize;

/** Btn **/
@property(nonatomic,strong) UIButton *cleanbtn;
@end

@implementation DoorSystemSDCardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setNavigationStyle];
    
    [self masLayoutSubview];
    self.titleArray = @[@"储存空间大小",@"可用空间大小"];
    
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    self.totalSize = -1;
    self.freeSize = -1;
    [AppDelegate globalDelegate].cameraModel.delegate2 = self;
    SMsgAVIoctrlDeviceInfoReq *s = malloc(sizeof(SMsgAVIoctrlDeviceInfoReq));
    memset(s, 0, sizeof(SMsgAVIoctrlDeviceInfoReq));
    
    [[AppDelegate globalDelegate].cameraModel sendIOCtrlToChannel:0
                                                             Type:IOTYPE_USER_IPCAM_DEVINFO_REQ
                                                             Data:(char *)s
                                                         DataSize:sizeof(SMsgAVIoctrlDeviceInfoReq)];
    free(s);
    
}
- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    [AppDelegate globalDelegate].cameraModel.delegate2 = nil;
    
}
- (void)setNavigationStyle{
    
    [self setLeftButtonImage:[UIImage imageNamed:@"icon_back_nor"]];
    [self setNavigationTitle:@"储存卡设置" titleColor:[UIColor colorWithRed:210/255.0 green:177/255.0 blue:104/255.0 alpha:1]];
}

#pragma mark - tableView Datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 2;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 54.f;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return CGFLOAT_MIN;
    
}

#pragma mark - tableView Delegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        [cell.textLabel setFont:[UIFont systemFontOfSize:15.f]];
        [cell.detailTextLabel setFont:[UIFont systemFontOfSize:15.f]];
    }
    
    cell.textLabel.text = self.titleArray[indexPath.row];
    
    if (self.totalSize>10) {
        
        self.cleanbtn.enabled = YES;
        [self.cleanbtn setTitle:@"清空储存卡" forState:UIControlStateNormal];
        
    }else{
        
        self.cleanbtn.enabled = NO;
        [self.cleanbtn setTitle:@"获取信息中..." forState:UIControlStateNormal];
    }

    if (indexPath.row == 0) {
        
        
        if (self.totalSize/1024>=1) {
            
            cell.detailTextLabel.text = (self.totalSize >= 0) ? [NSString stringWithFormat:@"%.2f GB", (float)self.totalSize/1024] : nil;
            
        }else{
            
            cell.detailTextLabel.text = (self.totalSize >= 0) ? [NSString stringWithFormat:@"%ld MB", (long)self.totalSize] : nil;
            
        }
        
    }else if(indexPath.row == 1){
        
        
        if (self.freeSize/1024>=1) {
            
            cell.detailTextLabel.text = (self.freeSize >= 0) ? [NSString stringWithFormat:@"%.2f GB", (float)self.freeSize/1024] : nil;
            
        }else{
            
            cell.detailTextLabel.text = (self.freeSize >= 0) ? [NSString stringWithFormat:@"%ld MB", (long)self.freeSize] : nil;
            
        }
        
        
    }
    
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
}

#pragma mark - CameraDelegate Methods

- (void)camera:(MyCamera *)camera_ _didReceiveIOCtrlWithType:(NSInteger)type Data:(const char *)data DataSize:(NSInteger)size {
    
    if (camera_ == [AppDelegate globalDelegate].cameraModel && type == IOTYPE_USER_IPCAM_FORMATEXTSTORAGE_RESP) {
        
        SMsgAVIoctrlFormatExtStorageResp *s = (SMsgAVIoctrlFormatExtStorageResp *)data;
        
        if (s->result == 0){
            [SVProgressHUD showSuccessWithStatus:@"格式化成功"];
            SMsgAVIoctrlDeviceInfoReq *s1 = malloc(sizeof(SMsgAVIoctrlDeviceInfoReq));
            memset(s1, 0, sizeof(SMsgAVIoctrlDeviceInfoReq));
            
            [[AppDelegate globalDelegate].cameraModel sendIOCtrlToChannel:0
                                                                     Type:IOTYPE_USER_IPCAM_DEVINFO_REQ
                                                                     Data:(char *)s
                                                                 DataSize:sizeof(SMsgAVIoctrlDeviceInfoReq)];
            free(s1);
            
        }
        else{
            [SVProgressHUD showSuccessWithStatus:@"格式化失败"];
            
        }
        
        
    }
    __weak typeof(self) weakself = self;
    
    if (camera_ == [AppDelegate globalDelegate].cameraModel && type == IOTYPE_USER_IPCAM_DEVINFO_RESP) {
        
        SMsgAVIoctrlDeviceInfoResp *structDevInfo = (SMsgAVIoctrlDeviceInfoResp*)data;
        weakself.totalSize = structDevInfo->total;
        weakself.freeSize = structDevInfo->free;
        
        [weakself.tableView reloadData];
    }
    
    
    
}
#pragma mark - --------------------------清除所有信息--------------------------
- (void)cleanEvent{
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"警告" message:@"是否格式化SD卡" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        if ([AppDelegate globalDelegate].cameraModel != nil) {
            
            SMsgAVIoctrlFormatExtStorageReq *s = malloc(sizeof(SMsgAVIoctrlFormatExtStorageReq));
            memset(s, 0, sizeof(SMsgAVIoctrlFormatExtStorageReq));
            s->storage = 0;
            
            [[AppDelegate globalDelegate].cameraModel sendIOCtrlToChannel:0
                                                                     Type:IOTYPE_USER_IPCAM_FORMATEXTSTORAGE_REQ
                                                                     Data:(char *)s
                                                                 DataSize:sizeof(SMsgAVIoctrlFormatExtStorageReq)];
            free(s);
            
        }
        
        
    }];
    
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];
    [self presentViewController:alertController animated:YES completion:nil];
    
}
- (void)masLayoutSubview{
    
    __weak UIView *superView = self.view;
    
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(superView);
        
    }];
    [self.cleanbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(superView.mas_centerX);
        make.centerY.mas_equalTo(superView.mas_centerY).mas_offset(-80);
        
    }];
}

- (UIButton *)cleanbtn{
    
    if (!_cleanbtn) {
        __weak UIView *superView = self.view;
        
        _cleanbtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [superView addSubview:_cleanbtn];
        
        [_cleanbtn setBackgroundImage:[UIImage imageNamed:@"login_button_nor"] forState:UIControlStateNormal];
        [_cleanbtn setBackgroundImage:[UIImage imageNamed:@"login_button_sel"] forState:UIControlStateHighlighted];
        [_cleanbtn addTarget:self action:@selector(cleanEvent) forControlEvents:UIControlEventTouchUpInside];
        [_cleanbtn setTitle:@"清空储存卡" forState:UIControlStateNormal];
    }
    return _cleanbtn;
}
- (UITableView *)tableView{
    
    if (!_tableView) {
        
        __weak UIView *superView = self.view;
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.scrollEnabled = NO;
        [superView addSubview:_tableView];
        //        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;//隐藏tableView的分割线
        //        _tableView.scrollEnabled = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
    }
    
    return _tableView;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
